package com.nanjia.boot.brand.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.brand.converter.BrandInfoConverter;
import com.nanjia.boot.brand.mapper.BrandInfoMapper;
import com.nanjia.boot.brand.model.entity.BrandInfo;
import com.nanjia.boot.brand.model.form.BrandInfoForm;
import com.nanjia.boot.brand.model.query.BrandInfoQuery;
import com.nanjia.boot.brand.model.vo.BrandInfoVO;
import com.nanjia.boot.brand.service.BrandInfoService;
import com.nanjia.boot.common.enums.StatusEnum;
import com.nanjia.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 品牌信息服务实现类
 *
 * @author 周洋
 * @since 2025-07-04 15:18
 */
@Service
@RequiredArgsConstructor
public class BrandInfoServiceImpl extends ServiceImpl<BrandInfoMapper, BrandInfo> implements BrandInfoService {

    private final BrandInfoConverter brandInfoConverter;

    private final BrandInfoMapper brandInfoMapper;

    /**
    * 获取品牌信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<BrandInfoVO>} 品牌信息分页列表
    */
    @Override
    public IPage<BrandInfoVO> getBrandInfoPage(BrandInfoQuery queryParams) {
        Page<BrandInfoVO> pageVO = this.baseMapper.getBrandInfoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取品牌信息表单数据
     *
     * @param id 品牌信息ID
     * @return 品牌信息表单数据
     */
    @Override
    public BrandInfoForm getBrandInfoFormData(Long id) {
        BrandInfo entity = this.getById(id);
        return brandInfoConverter.toForm(entity);
    }
    
    /**
     * 新增品牌信息
     *
     * @param formData 品牌信息表单对象
     * @param ipAddr
     * @return 是否新增成功
     */
    @Override
    public boolean saveBrandInfo(BrandInfoForm formData, String ipAddr) {
        LambdaQueryWrapper<BrandInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(BrandInfo::getId);
        List<BrandInfo> list = this.list(queryWrapper);
        int x = 0;
        if (CollectionUtils.isNotEmpty(list)) {
            x = Integer.parseInt(list.get(0).getBrandCode());
        }
        BrandInfo entity = brandInfoConverter.toEntity(formData);
        entity.setBrandCode(String.format("%04d", x + 1));
        entity.setCreateBy(SecurityUtils.getUserStrId());
        entity.setCreateBy(SecurityUtils.getUserStrId());
        entity.setCreateIp(ipAddr);
        entity.setCreateTime(LocalDateTime.now());

        return this.save(entity);
    }
    
    /**
     * 更新品牌信息
     *
     * @param id       品牌信息ID
     * @param formData 品牌信息表单对象
     * @param ipAddr
     * @return 是否修改成功
     */
    @Override
    public boolean updateBrandInfo(Long id, BrandInfoForm formData, String ipAddr) {
        BrandInfo entity = brandInfoConverter.toEntity(formData);
        entity.setId(id);
        entity.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
        entity.setUpdateIp(ipAddr);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }
    
    /**
     * 删除品牌信息
     *
     * @param ids 品牌信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteBrandInfos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的品牌信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        List<BrandInfo> list = new ArrayList<>();
        for (Long l : idList) {
            BrandInfo brandInfo = new BrandInfo();
            brandInfo.setId(l);
            brandInfo.setUpdateTime(LocalDateTime.now());
            brandInfo.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
            brandInfo.setBrandStatus(StatusEnum.DELETE.getValue());
            list.add(brandInfo);
        }
        return this.updateBatchById(list, idList.size());
    }

    @Override
    public List<BrandInfo> getBrandInfoVOList(BrandInfoQuery queryParams) {
        Map<String, Object> map = new HashMap<>();
        map.put("brand_status",queryParams.getBrandStatus());
        return brandInfoMapper.selectByMap(map);
    }
}
